package RA.Doa;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	private static final String DB_NAME = "Radb.db";
	private static final String TBL_NAME = "RaTable";
	private static final String CREATE_TBL = " create table "
			+ " RaTable(Id integer primary key autoincrement,Name text,MsgContent text, OnMode text,BeginTime text,EndTime text,IsRing integer,IsVibrating integer,IsMsg integer,IsClose integer,IsActive integer,IsStartrun integer ) ";
	
	private SQLiteDatabase db;

	DBHelper(Context c) {
		super(c, DB_NAME, null, 2);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		db.execSQL(CREATE_TBL);
	}

//Insert a single record
	public void insert(ContentValues values) {
		SQLiteDatabase db = getWritableDatabase();
		db.insert(TBL_NAME, null, values);
		db.close();
	}
	
	
//Query the configurations from database
	public Cursor query(int Id)
	{
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(TBL_NAME, null, "Id=?", new String[] { String.valueOf(Id) }, null, null, null);
		return c;
	}
	
	public Cursor query(String Name)
	{
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(TBL_NAME, null, "Name=?", new String[] { Name }, null, null, null);
		return c;
	}
	public Cursor queryByMode(String Mode)
	{
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(TBL_NAME, null, "OnMode=?", new String[] { Mode }, null, null, null);
		return c;
	}
	public Cursor query() {
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
		return c;
	}
	
//Delete a single record by its Id
	public void del(int Id) {
		if (db == null)
			db = getWritableDatabase();
		db.delete(TBL_NAME, "Id=?", new String[] { String.valueOf(Id) });
	}

//Delete a single record by its Name
	public void del(String Name) {
		if (db == null)
			db = getWritableDatabase();
		db.delete(TBL_NAME, "Name=?", new String[] { Name });
	}

//Update a single record by its Id
	public void update(int Id,ContentValues values)
	{
		SQLiteDatabase db = getWritableDatabase();
		db.update(TBL_NAME, values, "Id=?", new String[] { String.valueOf(Id) });
	}
	
	public void close() {
		if (db != null)
			db.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
}
